<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="项目名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item label="项目类别" prop="category">
  <el-select
    v-model="queryParams.category"
    placeholder="请选择项目类别"
    clearable
    @change="handleQuery"
  >
    <el-option label="电气故障" value="0"></el-option>
    <el-option label="管道问题" value="1"></el-option>
    <el-option label="设备故障" value="2"></el-option>
  </el-select>
</el-form-item>

      <el-form-item label="创建时间" prop="createdAt">
        <el-date-picker clearable
          v-model="queryParams.createdAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="报修ID" prop="requestId">
        <el-input
          v-model="queryParams.requestId"
          placeholder="请输入报修请求ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


            <el-form-item label="材料名称" prop="name" >
             <el-select v-model="queryParams.materialId" placeholder="请选择材料"
            clearable
              @change="handleQuery">
                <el-option
                  v-for="item in materialOptions"
                  :key="item.materialId"
                  :label="item.name"
                  :value="item.materialId"
                ></el-option>
              </el-select>
          </el-form-item>
      

      
      <el-form-item label="更新时间" prop="updateAt">
        <el-date-picker clearable
          v-model="queryParams.updateAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择更新时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="创建人" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入创建人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ycl:item:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ycl:item:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ycl:item:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ycl:item:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="itemList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="项目ID" align="center" prop="itemId" />
      <el-table-column label="项目名称" align="center" prop="name" />
      <el-table-column label="报修ID" align="center" prop="requestId" />
      <el-table-column label="项目描述" align="center" prop="description" width="200"/>
      
      <el-table-column prop="category" label="项目类别" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.category_type" :value="scope.row.category"/>
        </template>
      </el-table-column>

      <el-table-column label="材料名称" align="center" prop="mname" />
      
      <el-table-column label="申请数量" align="center" prop="total" />
      
      <el-table-column prop="status" label="状态" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.item_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      
      <el-table-column label="创建时间" align="center" prop="createdAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateAt, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="userName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
        
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ycl:item:edit']"
                        v-show="scope.row.status==0"
          >修改</el-button>

           <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ycl:item:edit']"
                        v-show="scope.row.status==1"
          >打回修改</el-button>
          
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ycl:item:remove']"
                        v-show="scope.row.status==0"
          >删除</el-button>
          
          <el-button
            size="mini"
            type="text"
            icon="el-icon-single"
            @click="handleItemSubmit(scope.row.itemId)"
            v-hasPermi="['ycl:item:itemSubmit']"
            v-show="scope.row.status==0|scope.row.status==1"
          >提交项目申请</el-button>
          
          
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改报修项目对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="项目名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入项目名称" />
        </el-form-item> 
        
       <el-form-item label="报修ID">
             <el-select v-model="form.requestId" placeholder="请选择报修请求ID">
                <el-option
                  v-for="item in requestOptions"
                  :key="item.requestId"
                  :label="item.name"
                  :value="item.requestId"
                ></el-option>
              </el-select> 
        </el-form-item>

        <el-form-item label="项目描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>

        
               <el-form-item label="项目类别">
             <el-select v-model="form.category" placeholder="请项目类别">
                <el-option
                  v-for="dict in dict.type.category_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
        </el-form-item>
        
      

        <el-form-item label="材料名称">
             <el-select v-model="form.materialId" placeholder="请选择材料名称">
                <el-option
                  v-for="item in materialOptions"
                  :key="item.materialId"
                  :label="item.name"
                  :value="item.materialId"
                ></el-option>
              </el-select> 
        </el-form-item>
        
        <el-form-item label="申请数量" prop="total">
          <el-input v-model="form.total" placeholder="请输入申请数量" />
        </el-form-item>



      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getAllRequests, listItem, getItem, delItem, addItem, updateItem,submitItem } from "@/api/ycl/item";
import {getAllMaterials} from "@/api/ycl/material";

export default {
  name: "Item",
  dicts: ["category_type","item_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      
      requestOptions:[],
      materialOptions:[],
      
      // 报修项目表格数据
      itemList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        description: null,
        category: null,
        createdAt: null,
        requestId: null,
        materialId: null,
        total: null,
        flowInstantId: null,
        status: null,
        updateAt: null,
        userName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询报修项目列表 */
    getList() {
      this.loading = true;
      listItem(this.queryParams).then(response => {
        this.itemList = response.rows;
        this.total = response.total;
        this.loading = false;
        console.log(this.itemList);
      });
             getAllMaterials().then(
        response=>{
          
          this.materialOptions=response.data;
        }
      );

    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        itemId: null,
        name: null,
        description: null,
        category: null,
        createdAt: null,
        requestId: null,
        materialId: null,
        total: null,
        flowInstantId: null,
        status: null,
        updateAt: null,
        userName: null
 
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
      console.log(this.queryParams);
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.itemId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加报修项目";
      
        getAllRequests().then(
        response=>{
          console.log(response.data);
          this.requestOptions=response.data;
        }
      );
      
             getAllMaterials().then(
        response=>{
          console.log(response.data);
          this.materialOptions=response.data;
        }
      );
      
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const itemId = row.itemId || this.ids
      getItem(itemId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改报修项目";
      });
      
      getAllRequests().then(
        response=>{
          console.log(response.data);
          this.requestOptions=response.data;
        }
      );
       getAllMaterials().then(
        response=>{
          console.log(response.data);
          this.materialOptions=response.data;
        }
      );
      
      
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.itemId != null) {
            updateItem(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addItem(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const itemIds = row.itemId || this.ids;
      this.$modal.confirm('是否确认删除报修项目编号为"' + itemIds + '"的数据项？').then(function() {
        return delItem(itemIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ycl/item/export', {
        ...this.queryParams
      }, `item_${new Date().getTime()}.xlsx`)
    },


    //提交项目申请
    handleItemSubmit(itemId){
        submitItem(itemId).then(response => {
          console.log(response)
          this.getList();
          this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + "提交申请成功！" + "</div>", "提示框", { dangerouslyUseHTMLString: true });
       });
   }
    
  }
};
</script>
